package com.chance.cc.crawler.prod.command.job.domain.commerce.du.product;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterInfo;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.queue.crawler.CrawlerQueueConfig;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.commerce.du.DuCommonCrawlerJobScript;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;

/**
 * @author lt
 * @version 1.0
 * @date 2021-06-22 10:35:35
 * @email okprog@sina.com
 */
public class DuProductFindListCrawlerSchedulerJob extends DuCommonCrawlerJobScript {

    public static final String metaSite = "product";
    public static final String startUrl = "api/v1/h5/commodity/fire/search/doCategoryDetail";
    public static final String startUrlFile = "api/v1/h5/commodity/fire/search/doCategoryDetail#file";

    public static void main(String[] args) {
        publishCrawlerScheduleJobInfo();
    }

    /**
     * 发布任务信息
     * @return
     */
    public static CrawlerJob publishCrawlerScheduleJobInfo(){

        CrawlerJob crawlerJob = crawlerScheduler();

        //发布定时任务
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(crawlerJob));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("Du商品信息每日列表更新采集任务");
        crawlerScheduleJob.setCrawlerKey(crawlerJob.generateCrawlerKey());
        HttpPage httpPage = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布任务：" + httpPage.getRawText());
        return crawlerJob;
    }

    public static CrawlerJob crawlerScheduler(){

        //文章采集
        CrawlerRequestRecord articleCrawler = doSearchNewsCrawler();

        //配置结果队列信息
        String resultAddQueueName = "result-du_app-spuIds";
        CrawlerQueueConfig redisAddResultQueue = new CrawlerQueueConfig(
                resultAddQueueName,
                CrawlerQueueConfig.Content.result,
                CrawlerQueueConfig.Storage.redis,
                CrawlerQueueConfig.Structure.list);
        //配置结果队列信息
        String resultDelQueueName = "delResult-du_app-spuIds";
        CrawlerQueueConfig redisDelResultQueue = new CrawlerQueueConfig(
                resultDelQueueName,
                CrawlerQueueConfig.Content.result,
                CrawlerQueueConfig.Storage.redis,
                CrawlerQueueConfig.Structure.list);

        //同步到mysql callback record
        CrawlerRequestRecord syncAddCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("du_app-result-add-sync", turnPageItem)
                .httpUrl(metaServiceHttpPrefix + "/crawler/domain/common/api/v1/"+domain+"/search/results/sync?resultQueue="
                        +resultAddQueueName + "&site=" + metaSite)
                .requestLabelTag(supportCallback)
                .requestLabelTag(internalDownload)
                .build();

        CrawlerJob crawlerJob = CrawlerJob.builder()
                .triggerInfo(domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-", domain,metaSite,"findList", CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith("-", "crawler", domain, metaSite, "findList", "queue")))
                .queueResultPipeline("redis_add_result",redisAddResultQueue)
                .queueResultPipeline("redis_del_result",redisDelResultQueue)
                .crawlerJobThreadNumber(20)
                .requestRecord(articleCrawler)
                .supportRecord(getKwRecord(metaSite))
                .supportRecord(syncAddCrawlerRecord)
                .build();
        return crawlerJob;
    }

    /**
     * 初始record
     * @return
     */
    public static CrawlerRequestRecord doSearchNewsCrawler(){
        FilterInfo turnPageFilterInfo = new FilterInfo();
        turnPageFilterInfo.setFilter(CrawlerEnum.CrawlerRecordFilter.count);
        turnPageFilterInfo.setCountTotalNum(100);

        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, turnPage)
                .httpUrl(startUrl)
                .releaseTime(System.currentTimeMillis())
                .needWashed(false)
                .needParsed(false)
                .filter(CrawlerEnum.CrawlerRecordFilter.key)
                .addFilterInfo(FilterUtils.redisFilterKeyInfo("filter-du_app-spuIds"))
                .turnPageFilterInfo(turnPageFilterInfo)
                .proxy(proxy)
                .build();

        requestRecord.setDownload(false);
        requestRecord.setSkipPipeline(true);
        requestRecord.tagsCreator().bizTags().addSite(metaSite);
        requestRecord.tagsCreator().bizTags().addCustomKV("crawler_type","findList");
        requestRecord.tagsCreator().bizTags().addCustomKV("crawler_sort_type","3");
//        requestRecord.tagsCreator().bizTags().addCustomKV("filePath","/data/chance_crawler_test/data/du/urls.json");
        return requestRecord;
    }

    public static CrawlerRequestRecord getKwRecord(String metaSite){
        //keyword record
        return CrawlerRequestRecord.builder()
                .startPageRequest(StringUtils.joinWith("-",domain, metaSite),turnPageItem)
                .httpUrl("http://"+metaServiceIp+":"+metaServicePort+"/v1/meta/"+domain+"/keys?site=" + metaSite)
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();
    }
}
